package com.aoxin.controller.activity;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aoxin.model.activity.SlfSignin;
import com.aoxin.service.inter.activity.SigninService;
import com.aoxin.util.AjaxRes;
import com.aoxin.util.Const;
import com.aoxin.util.ThreadLocalUtil;

/**
 * 签到 Controller
 * 
 * @author Melody
 * @date 2018年4月28日 下午5:24:14
 */
@Controller
@ResponseBody
@RequestMapping("/web/signin")
public class SigninController {

	@Autowired
	private SigninService signinService;

	@RequestMapping("/insertSignin")
	public AjaxRes insertSignin(AjaxRes ajaxRes, SlfSignin slfSignin) {
		try {
			slfSignin.setUserId(ThreadLocalUtil.getUser().getUserId());
			slfSignin.setUserName(ThreadLocalUtil.getUser().getName());
			this.signinService.insertSignin(slfSignin);
			ajaxRes.setSucceedMsg(Const.SAVE_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	@RequestMapping("/updateSignin")
	public AjaxRes updateSignin(AjaxRes ajaxRes, SlfSignin slfSignin) {
		try {
			this.signinService.updateSignin(slfSignin);
			ajaxRes.setSucceedMsg(Const.UPDATE_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	@RequestMapping("/deleteSignins")
	public AjaxRes deleteSignins(AjaxRes ajaxRes, @RequestParam("signinIds") List<Integer> signinIds) {
		try {
			this.signinService.deleteSignins(signinIds);
			ajaxRes.setSucceedMsg(Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	@RequestMapping("/updateSigninStates")
	public AjaxRes updateSigninStates(AjaxRes ajaxRes, @RequestParam("state") Integer state, @RequestParam("signinIds") List<Integer> signinIds) {
		try {
			this.signinService.updateSigninStates(state, signinIds);
			ajaxRes.setSucceedMsg(Const.UPDATE_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	@RequestMapping("/getSigninList")
	public AjaxRes getSigninList(AjaxRes ajaxRes) {
		try {
			List<SlfSignin> slfSignins = this.signinService.getSigninList();
			ajaxRes.setSucceed(slfSignins, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
}
